define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'medicine/medicines/index' + location.search,
                    //add_url: 'medicine/medicine/add',
                    //edit_url: 'medicine/medicine/edit',
                    //del_url: 'medicine/medicine/del',
                    for_url: 'medicine/medicines/detail',
                    multi_url: 'medicine/medicines/multi',
                    import_url: 'medicine/medicines/import',
                    table: 'medicines',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: '',
                commonSearch: true,
                searchFormVisible: true,
                visible: false,
                showToggle: false,
                showColumns: false,
                search:false,
                showExport: false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('序号'), formatter: function(value, row, index){
                                return ++index;
                            },operate: false},
                        {field: 'code', title: __('产品编码'), operate: false},
                        {field: 'medicine_name', title: __('药品名'), operate: 'LIKE'},
                        //{field: 'unit', title: __('单位'), operate: false},
                        {field: 'specification', title: __('规格型号'), operate: false},
                        {field: 'supplier_name', title: __('供应商'), operate: false},
                        {field: 'model_number', title: __('药品型号'), operate: false},
                        {field: 'type', title: __('药品分类'),searchList: {"药品":__('药品'),"耗材":__('耗材'),"保健品":__('保健品')} },
                        //{field: 'image', title: __('图片'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        //{field: 'status', title: __("状态"), searchList: {"0":__('未发布'),"1":__('已发布')}, formatter: Table.api.formatter.status},
                        //{field: 'create_time', title: __('创建时间'), operate:false, addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'operate', title: __('Operate'), table: table,
                        //     events: Table.api.events.operate,
                        //     buttons: [{
                        //         name: 'detail',
                        //         text: __('Detail'),
                        //         icon: 'fa fa-list',
                        //         classname: 'btn btn-info btn-xs btn-detail btn-dialog',
                        //         url: 'medicine/medicine/detail'
                        //     }],
                        //     formatter: Table.api.formatter.operate
                        // }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'test/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'title', title: __('Title'), align: 'left'},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'test/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'test/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        detail: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"),function (data,ret) {
                    //console.log(ret);

                    if (ret.code == 1){
                        //提示及关闭当前窗口
                        var msg = ret.hasOwnProperty("msg") && ret.msg !== "" ? ret.msg : __('Operation completed');
                        parent.parent.Toastr.success(msg);
                        parent.$(".btn-refresh").trigger("click");
                        parent.parent.$(".btn-refresh").trigger("click");
                        //var index = parent.Layer.getFrameIndex(window.name);

                        //parent.parent.Layer.close(index);

                        parent.parent.Layer.closeAll();

                        // setTimeout(function(){  //使用  setTimeout（）方法设定定时2000毫秒
                        //     window.parent.parent.location.reload();
                        // },1500);
                        return false;
                    }

                });

            }
        }
    };
    return Controller;
});